package com.lmx.repoDomain.repository;

import io.katharsis.queryParams.QueryParams;
import io.katharsis.repository.annotations.JsonApiFindAll;
import io.katharsis.repository.annotations.JsonApiResourceRepository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.lmx.common.util.CommonUtil;
import com.lmx.persistence.service.StatService;
import com.lmx.repoDomain.tmodel.StatQuery;

@JsonApiResourceRepository(StatQuery.class)
@Component
public class StatQueryRepository extends BaseRepository<StatQuery> {

	@Autowired
	protected StatService statService;

	@PostConstruct
	public void init() {
		dslJpaService.setEntityClass(StatQuery.class);
	}

	@Override
	@JsonApiFindAll
	public Iterable<StatQuery> findAll(final QueryParams requestParams) {
		List<StatQuery> listData = new ArrayList<>();
		Map<String, String> map = CommonUtil.getKeyMapByFilter(requestParams);
		String statType;
		// .typecode=statType11@$orstatType12@$orstatType13@$orstatType14@$orstatType15@$orstatType16@$orstatType17@$or
		if (map.containsKey("statType.typecode") && !"".equals(map.get("statType.typecode"))) {
			statType = map.get("statType.typecode");
			String[] types = statType.replace("$or", "").split("@");
			Integer beginTime = Integer.parseInt(map.get("beginTime"));
			Integer endTime = Integer.parseInt(map.get("endTime"));
			String durType = map.get("durType");// 统计区间,1:年 2季 3月 4周 5日
			for (String type : types) {
				List tempList = new ArrayList<>();
				if ("4".equals(durType)) {
					// tempList=statService.statQueryForWeek(type,beginTime,endTime);
				} else if ("3".equals(durType)) {
					tempList = statService.statQueryForMonth(type, beginTime, endTime);
				} else if ("2".equals(durType)) {
					tempList = statService.statQueryForQuarter(type, beginTime, endTime);
				} else if ("1".equals(durType)) {
					tempList = statService.statQueryForYear(type, beginTime, endTime);
				} else {
					/*
					 * if ("statType13".equals(type)) { try { tempList =
					 * statService.queryForYear(type, beginTime, endTime); }
					 * catch (ParseException e) { // TODO Auto-generated catch
					 * block e.printStackTrace(); } }
					 */
					tempList = statService.statQueryForDay(type, beginTime, endTime);
				}
				if (tempList != null && tempList.size() > 0) {
					listData.addAll(tempList);
				}
			}
			return listData;
		} else if (map.containsKey("queryType") && "statService".equals(map.get("queryType"))) {
			List tempList = new ArrayList<>();
			Integer beginTime = Integer.parseInt(map.get("beginTime"));
			Integer endTime = Integer.parseInt(map.get("endTime"));
			String durType = map.get("durType");// 统计区间,1:年 2季 3月 4周 5日
			String staffIds = map.get("staffId");// 统计区间,1:年 2季 3月 4周 5日
			staffIds = "(" + staffIds.subSequence(0, staffIds.length() - 1) + ")";
			if ("4".equals(durType)) {
				// tempList=statService.statQueryForWeek(type,beginTime,endTime);
			} else if ("3".equals(durType)) {
				tempList = statService.statServiceQueryForMonth(staffIds, beginTime, endTime);
			} else if ("2".equals(durType)) {
				tempList = statService.statServiceQueryForQuarter(staffIds, beginTime, endTime);
			} else if ("1".equals(durType)) {
				tempList = statService.statServiceQueryForYear(staffIds, beginTime, endTime);
			} else {
				tempList = statService.statServiceQueryForDay(staffIds, beginTime, endTime);
			}
			if (tempList != null && tempList.size() > 0) {
				listData.addAll(tempList);
			}
			return listData;
		} else if (map.containsKey("queryType") && "queryCustomer".equals(map.get("queryType"))) {
			List tempList = new ArrayList<>();
			listData.add(statService.statOntime(map.get("queryType"), 0, 0));
			return listData;
		} else {
			return super.findAll(requestParams);
		}
	}

}
